/*
 * Created on Feb 17, 2005
 * Created by dfhuynh
 */
package edu.mit.simile.longwell.ui.command;

import java.util.Date;

import org.apache.velocity.VelocityContext;

import edu.mit.simile.longwell.LongwellServer;
import edu.mit.simile.longwell.Profile;
import edu.mit.simile.longwell.ui.FlairMessage;
import edu.mit.simile.longwell.ui.FlairURL;
import edu.mit.simile.longwell.ui.InjectionManager;

/**
 * @author dfhuynh
 */
abstract public class CommandBase implements IFlairCommand {
	final protected InjectionManager m_injectionManager;

	public CommandBase(InjectionManager injectionManager) {
		m_injectionManager = injectionManager;
	}

	protected InjectionManager createInjections(FlairMessage msg) {
		return (InjectionManager) m_injectionManager.clone();
	}

	protected VelocityContext createContext(FlairMessage msg) {
		VelocityContext vcContext = new VelocityContext();
		FlairURL		url = new FlairURL(msg);
		Profile			profile = msg.getProfile();

		vcContext.put("longwell", 		LongwellServer.getServer());
		vcContext.put("profile", 		profile);
		vcContext.put("schemaModel", 	profile.getSchemaModel());

		vcContext.put("msg", 			msg);
		vcContext.put("url", 			url);
		vcContext.put("outerURL", 		url);
		vcContext.put("contextPath", 	url.getContextPath());
		vcContext.put("resourcePath", 	url.getContextPath() + "/resources");
		vcContext.put("locale", 		"");

		vcContext.put("currentTime",	new Date());
		vcContext.put("utilities",		new Utilities());

		vcContext.put("injections", 	createInjections(msg));

		vcContext.put("response", 		msg.m_response);

		return vcContext;
	}
}
